package com.ruoyi.web.controller.question;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.domain.question.Subject;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.question.service.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping(value = "/subject")
public class SubjectController extends BaseController {

   @Autowired
   protected ISubjectService biz;

   @GetMapping("/list")
   public TableDataInfo list(Subject dir)
   {
      startPage();
      List<Subject> dirs = biz.selectList(dir);
      return getDataTable(dirs);
   }

   @GetMapping(value = "/info/{id}")
   public AjaxResult getInfo(@PathVariable Long id)
   {
      return success(biz.findById(id));
   }

   @Log(title = "科目管理", businessType = BusinessType.INSERT)
   @PostMapping
   public AjaxResult addOrEdit(@Validated @RequestBody Subject record)
   {
      if(record.getId() == null) {
         record.setCreateBy(getUsername());
         record.setCreateTime(DateUtils.getNowDate());
      }
      record.setUpdateBy(getUsername());
      record.setUpdateTime(DateUtils.getNowDate());
      return toAjax(biz.saveRecord(record));
   }

   @Log(title = "科目管理", businessType = BusinessType.DELETE)
   @DeleteMapping("/{id}")
   public AjaxResult remove(@PathVariable Long id)
   {
      Subject _r = biz.selectByPrimaryKey(id);
      return toAjax(biz.delete(_r));
   }

}
